package com.sunxueqi.project.system.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunxueqi.common.constant.StatusConstants;
import com.sunxueqi.common.constant.UserConstants;
import com.sunxueqi.common.utils.KeyGenerate;
import com.sunxueqi.common.utils.SecurityUtils;
import com.sunxueqi.project.system.dao.UserMapper;
import com.sunxueqi.project.system.dao.UserRoleMapper;
import com.sunxueqi.project.system.service.IUserService;
import com.sunxueqi.project.system.vo.User;
import com.sunxueqi.project.system.vo.UserRole;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;

@Service
@Transactional
public class UserServiceImpl implements IUserService{

	@Autowired
	private UserMapper userMapper;
	
	@Value("${project.initial-password}")
	private String initialPassword;
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@Override
	public User selectUserByUsername(String username) {
		User user = userMapper.selectUserByUsername(username);
		return user;
	}

	@Override
	public List<User> selectUserList(User user) {
		user.setDeleteStatus(StatusConstants.NORMAL);
		return userMapper.selectList(user);
	}

	@Override
	public int deleteUserByIds(String[] idArray) {
		int result = 0;
		for(String id: idArray) {
			User user = new User();
			user.setId(id);
			user.setDeleteTime(DateUtil.date());
			user.setDeleteBy(SecurityUtils.getLoginUserId());
			user.setDeleteStatus(StatusConstants.DELETED);
			int count = userMapper.deleteByPrimaryKey(user);
			result += count;
		}
		return result;
	}

	@Override
	public int addUser(User user) {
		user.setId(KeyGenerate.getKeyId());
		user.setDeleteStatus(StatusConstants.NORMAL);
		user.setCreateBy(SecurityUtils.getLoginUserId());
		user.setCreateTime(new Timestamp(System.currentTimeMillis()));
		user.setPassword(SecurityUtils.encryptPassword(initialPassword));
		int count = userMapper.insert(user);
		insertUserRole(user);
		return count;
	}

	@Override
	public int updateUser(User user) {
		// 删除用户与角色关联
		String id = user.getId();
		userRoleMapper.deleteUserRoleByUserId(id);
		// 新增用户与角色关联
		insertUserRole(user);
		
		user.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		user.setUpdateBy(SecurityUtils.getLoginUserId());
		int count = userMapper.updateByPrimaryKey(user);
		return count;
	}

	@Override
	public int changeStatus(User user) {
		return userMapper.updateByPrimaryKey(user);
	}

	@Override
	public int resetPwd(User user) {
		user.setUpdateBy(SecurityUtils.getLoginUserId());
		user.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		user.setPassword(SecurityUtils.encryptPassword(initialPassword));
		return userMapper.updateByPrimaryKey(user);
	}
	
	/**
	 * 插入用户与角色关联关系
	 * @param user
	 */
	public void insertUserRole(User user) {
		List<String>  roleIds = user.getRoleIds();
		if(ObjectUtil.isNotNull(roleIds)) {
			List<UserRole> list = new ArrayList<UserRole>();
			for (String roleId : roleIds) {
				UserRole ur = new UserRole();
				ur.setId(KeyGenerate.getKeyId());
				ur.setUserId(user.getId());
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if(!list.isEmpty()) {
				userRoleMapper.addUserRole(list);
			}
		}
	}

	@Override
	public String checkUsernameUnique(User user) {
		int count = userMapper.selectUserCountByUsername(user.getUsername());
		return count > 0 ? UserConstants.NOT_UNIQUE : UserConstants.UNIQUE;
	}

	@Override
	public String checkPhoneUnique(User user) {
		int count = userMapper.selectUserCountByPhone(user.getPhone());
		return count > 0 ? UserConstants.NOT_UNIQUE : UserConstants.UNIQUE;
	}

}
